package com.zb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.DictionaryService;
import com.zb.entity.Dictionary;


@Scope("prototype")
@Component
@RequestMapping("/dictionary")
public class DictionaryController {

	@Resource
	private DictionaryService dictionaryService;
	
	// 用户信息显示
	@RequestMapping(params = "op=dict")
	@ResponseBody
	public Map<String, Object> showDictionary(Integer page, Integer rows){
		List<Dictionary> data =dictionaryService.findDictionary(page, rows);
		int count = dictionaryService.count();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", data);
		return map;
		
	}
	@RequestMapping(params = "op=dictchild")
	@ResponseBody
	public List<Dictionary> showChild(Integer secid){
		List<Dictionary> list=dictionaryService.findDictionaryChild(secid);
		return list;		
	}
	
	@RequestMapping(params = "op=updatedict")
	public String updatechilddict(Integer id, String name,Integer rank){
		Dictionary dictionary=new Dictionary();
		dictionary.setId(id);
		dictionary.setDictname(name);
		dictionary.setRank(rank); 
		dictionaryService.updateDictionary(dictionary);
		return "dictionary";	
	}
	
	@RequestMapping(params = "op=addsdict")
	@ResponseBody
	public Map<String,Object> addchilddict(String namechild,Integer rankchild,Integer secid){
		Map<String,Object> map = new HashMap<String, Object>();
		Dictionary dictionary=new Dictionary();
		dictionary.setDictname(namechild);
		dictionary.setSecid(secid);
		dictionary.setRank(rankchild); 
		dictionaryService.saveDictionaryChild(dictionary);
		map.put("success", true);
		map.put("message", "保存成功！");
		return map;
	}
	
	@RequestMapping(params = "op=deletedict")
	public String deleteDictionary(Integer id,ModelMap modelmap){
		List<Dictionary> list=dictionaryService.findDictionaryChild(id);
		if(list.size()>0){
			modelmap.put("e", "有外键引用，无法删除");
		}else{
			dictionaryService.deleteDictionary(id);
		}	
		return "dictionary";
		
	}
	@RequestMapping(params = "op=savedict")
	public String adddict(String names,Integer ranks){
		Dictionary dictionary=new Dictionary();
		dictionary.setDictname(names);
		dictionary.setRank(ranks);
		dictionaryService.saveDictionary(dictionary);
		return "dictionary";
		
	}
	
	
	
	
	
}
